package com.example.motion.config.vx;

import org.apache.commons.codec.binary.Base64;

import net.sf.json.JSONObject;


/**
 * 封装对外访问方法
 *
 * @author jxf
 */
public class WXCore {

    private static final String WATERMARK = "watermark";
    private static final String APPID = "appid";

    /**
     * 解密数据
     *
     * @return
     * @throws Exception
     */
    public static String decrypt(String appId, String encryptedData, String sessionKey, String iv) {
        String result = "";
        try {
            AES aes = new AES();
            byte[] resultByte = aes.decrypt(Base64.decodeBase64(encryptedData), Base64.decodeBase64(sessionKey), Base64.decodeBase64(iv));
            if (null != resultByte && resultByte.length > 0) {
                result = new String(WxPKCS7Encoder.decode(resultByte));
                JSONObject jsonObject = JSONObject.fromObject(result);
                String decryptAppid = jsonObject.getJSONObject(WATERMARK).getString(APPID);
                if (!appId.equals(decryptAppid)) {
                    result = "";
                }
            }
        } catch (Exception e) {
            result = "";
            e.printStackTrace();
        }
        return result;
    }


    public static void main(String[] args) throws Exception {
        //String str = "{\"stepInfoList\":[{\"timestamp\":1616428800,\"step\":0},{\"timestamp\":1616515200,\"step\":0},{\"timestamp\":1616601600,\"step\":0},{\"timestamp\":1616688000,\"step\":0},{\"timestamp\":1616774400,\"step\":0},{\"timestamp\":1616860800,\"step\":0},{\"timestamp\":1616947200,\"step\":0},{\"timestamp\":1617033600,\"step\":0},{\"timestamp\":1617120000,\"step\":0},{\"timestamp\":1617206400,\"step\":0},{\"timestamp\":1617292800,\"step\":0},{\"timestamp\":1617379200,\"step\":0},{\"timestamp\":1617465600,\"step\":0},{\"timestamp\":1617552000,\"step\":0},{\"timestamp\":1617638400,\"step\":0},{\"timestamp\":1617724800,\"step\":0},{\"timestamp\":1617811200,\"step\":0},{\"timestamp\":1617897600,\"step\":0},{\"timestamp\":1617984000,\"step\":0},{\"timestamp\":1618070400,\"step\":0},{\"timestamp\":1618156800,\"step\":0},{\"timestamp\":1618243200,\"step\":5509},{\"timestamp\":1618329600,\"step\":6381},{\"timestamp\":1618416000,\"step\":4303},{\"timestamp\":1618502400,\"step\":15363},{\"timestamp\":1618588800,\"step\":13613},{\"timestamp\":1618675200,\"step\":20894},{\"timestamp\":1618761600,\"step\":9061},{\"timestamp\":1618848000,\"step\":13214},{\"timestamp\":1618934400,\"step\":11834},{\"timestamp\":1619020800,\"step\":13708}],\"watermark\":{\"timestamp\":1619105235,\"appid\":\"wxb0a1892c6f3d5616\"}}";
        String appId = "wxb0a1892c6f3d5616";
        String encryptedData = "aTXRyyMXVo9gmRp8HDMQj6jHaXTUfkRQxeoEPr4+Sqnz+HC97r4TaiCFJtQAYCsfUX9kMtdcpEarEojeb76IStxTzrZNBaKirOofbR4WS5zd27ISzLq3pPASzm0AOgZ8MQHIhtpWVjKo7dKfhaoDIJ3n3tMyPYAKkgmYAIwzUMQCH+l4LH/MAdsTFFTtxrCpaTtR+6t62D6yYndbZYe6tev1XoDcwvARjgGD0FNZPkLWGW5u4A1OF7rkwyPh1LYHmnW9EgnDcnGaxQMNCHb0E/ZeHWN6M1TkkaUkyyfCv/um/kIPtTFCjrbcZcQY6qnt6YspSVqYnq138/PEjrWNfGkRBuLemro9W3bLxJrKp7dUgd5mpDtkI7rb44dRmQ1Rhl9kf3IYuWg3CLf5hfN9KFaNwm0sCEh12US+YBROdBDYCbv3Wm7ivfnve+lGueKs3bGngly5mmEIlJ4xrJJwRGhRq2Qr5R7fZxGbvwO2UfS9GHsCUNMaC4gH9Xpb++aEh7gPoHjNaQueyQAC5zBZzHVb3bDAScZL3ZfPU23ZBEPV/FZeSpvM2PtvutzcZVWdQ4mUI6JeGOKxJzXuJ2qlMsA9u0w22dlMFqctDaUrrHMDSohOOBHaBr2f1RT0omwr7SiFYPeQ745RgMVlt6S4cakb/k+D+ePa2utiEpjvKY2N49GP17BgeOaT0NuTP53FMPl5QCTdX7jX2XNWBkttR3meav4TCp7AHRZ1KtsxsKpom6gVAX5YIW2eu3hQIK4xCB+NynJNujN6NSEgzkjaWf+aZWW9CV+BohhjeL5AdfWHSgB9m8ZZ3TFxN7IbhRN7s9AJrtCiYKYxej4OoSOm8+jWAX9wS+VkuVoqRXoLV1zvBv2vpNAZukqw23t6tR1ZfslDUM4o0Kb4ODiSYWd4XTcz4VslEQN/h+46ixAcTtE+TNeGmMjRPBdLLi2GcOf/JR+XWFhnPLvp3Kat6gRmXOA6YMk76E09sZ5QZbzGerMnL+EgK+OODMixEFVBbOoQ46m39t3bODX+EMdXleOatNmUo3gJLvNZJDqHs+rmm94aJD5jpbWHLVOZ/we9wQuqZNNoqi6BCxZmHfI6Krjy6v4TWeTkIud+Sh76Zz4ClNxX3T2iiV3fZ/g43FFhFETZba91POEx4IZqivZCjXK21liD3sI/OO86xnhv8abcfiz72jwISF+YG1D9Im0xxCHA8EpqDL/BAKF4jdTCNnvs9VfD4C0YAC6ulWs1O3ThJyUj6MQIXdjn3Rde3FooMbt8BriBptXWWAY1zbariA29udRBUMckKJ/CGpNH6cpCe8HpJtblVdk85xlHdczL/DSiyVGhYKYkVFvMnSOmo6QHHhxrHa5x+bWlcibchpWk/VfM1OngB2b4YMCKSGaOFRLhomiCgzbygwxPuBce+jhTxr54yvgphJxRZ93PsoeX3VWD5fVkXD0E7kzUIfgzNLtBjNtoGYg5miuPmafTAVUEE/ePqI0gpNCMWAyIkIb1RzzIuZAGBxwEvLOELA3yi5GPOC2A9EfFk950QBE36XkAa4Xkk+H+URNG4Pllny/hiio=";
        String sessionKey = "bVX/tw8UpkvJ8UNJXvdfLA==";
        String iv = "YYy97TGahTVwwjrGC23Ppw==";
        System.out.println(decrypt(appId, encryptedData, sessionKey, iv));
    }
}
